﻿/* 
 * File:   txt analysis.h
 * Author: aple
 */

#ifndef _SIMP_TXT_ANALYSIS_H_
#define	_SIMP_TXT_ANALYSIS_H_

#include "stdint.h"

#ifdef	__cplusplus
extern "C" {
#endif

// 字符小于0x20判定为新行
// 字符0视为结束
// 多值解析中，任意非数据符号字符均视为分割
    enum txt_cmd_format_em
    {
        TXT_CMD_TYPE_END = 0, //结束-必须在列表尾部
        TXT_CMD_TYPE_CHAR  = 1,
        TXT_CMD_TYPE_SHORT = 2,
        TXT_CMD_TYPE_LONG  = 3,
        TXT_CMD_TYPE_FLOAT = 4,
        TXT_CMD_TYPE_STRING = 5,
        TXT_CMD_TYPE_NUMBER = 6, //纯数字命令，命令名字可以时任意字符串
        TXT_CMD_TYPE_NOP = 7,
    };
    typedef enum txt_cmd_format_em txt_cmd_format_type;

    struct txt_cmd_cfg_str
    {
		uint16_t attr;					//属性，仅外部使用
        char * cmd;						//命令名字 ，长度32字节以内，多余截取。 有效字符不包含数字，数字做序号
        txt_cmd_format_type format;		//命令格式
        void * des;						//值存储位置 
        uint8_t len_max;				//值最大长度 //静态解析接口最大为12
		void(*callback)(uint16_t cmd_site, const char * txt_src, uint16_t len, int16_t cmd_num);
		char * read_me;
    } ;

    typedef struct txt_cmd_cfg_str txt_cmd_cfg_type;

	//多值解析时，行截取80字节，多余舍弃。 值最多12个。使用局部缓存。
    uint16_t txt_cmd_analysis(const txt_cmd_cfg_type * cmd_list, const char * txt, uint16_t txt_len_max);
	int16_t txt_cmd_line_analysis(const txt_cmd_cfg_type * cmd_list, char * buff);

    //长行宽，多值解析
    typedef struct 
    {
        uint8_t * line_buff; //最小32
        int16_t * site_buff; //最小4
        int16_t line_max;
        int16_t site_max;
    } txt_cmd_analysis_ctr_type;
    
    //缓存由ctr提供
    /*-----------------------------------------------------------------------------------*
    * txt_auto_cmd_line_analysis
    * return cmd site
    *------------------------------------------------------------------------------------*/
    int16_t txt_auto_cmd_line_analysis(
        const txt_cmd_cfg_type * cmd_list, 
        txt_cmd_analysis_ctr_type *ctr
    );
    
    /*-----------------------------------------------------------------------------------*
    * txt_auto_cmd_analysis
    * return cmd amount
    *------------------------------------------------------------------------------------*/
    uint16_t txt_auto_cmd_analysis(
        const txt_cmd_cfg_type * cmd_list, 
        txt_cmd_analysis_ctr_type *ctr, 
        const char * txt, 
        uint16_t txt_len_max
    );
        
#ifdef	__cplusplus
}
#endif



#endif	/* _SIMP_TXT_ANALYSIS_H_ */

